package com.centit.framework.ip.oa.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.centit.framework.core.dao.CodeBook;
import com.centit.framework.core.dao.DictionaryMapUtils;
import com.centit.framework.jdbc.dao.BaseDaoImpl;
import com.centit.framework.jdbc.dao.DatabaseOptUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.centit.framework.ip.oa.po.UserBind;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;


/**
 * UserBindDao  Repository.
 * create by scaffold 2018-10-09
 *
 * @author codefan@sina.com
 *         用户绑定表用户绑定表，用于与投管系统的用户绑定
 */

@Repository
public class UserBindDao extends BaseDaoImpl<UserBind, java.lang.String> {

    public static final Log log = LogFactory.getLog(UserBindDao.class);

    @Override
    public Map<String, String> getFilterField() {
        if (filterField == null) {
            filterField = new HashMap<String, String>();

            filterField.put("recId", CodeBook.EQUAL_HQL_ID);


            filterField.put("userCode", CodeBook.EQUAL_HQL_ID);

            filterField.put("tzUserid", CodeBook.EQUAL_HQL_ID);

            filterField.put("isvalid", CodeBook.EQUAL_HQL_ID);

            filterField.put("createTime", CodeBook.EQUAL_HQL_ID);

            filterField.put("lastUpdateTime", CodeBook.EQUAL_HQL_ID);

            filterField.put("lastUpdateUserCode", CodeBook.EQUAL_HQL_ID);

        }
        return filterField;
    }

    public void updateTokenToUser(String userCode, String token) {
        String sql = "update f_userinfo t set t.extjsoninfo = ? where user_code = ?";
        DatabaseOptUtils.doExecuteSql(this, sql, new Object[]{token, userCode});
    }

    public UserBind getUserByTzUserId(String tzUserId) {
        String sql = "select * from f_user_bind t where t.tz_userid = ? and t.isvalid ='T' ";
        JSONArray jsonArray = DatabaseOptUtils.listObjectsBySqlAsJson(this, sql, new Object[]{tzUserId});
        if (jsonArray != null && jsonArray.size() > 0) {
            List<UserBind> userBinds = JSONObject.parseArray(jsonArray.toJSONString(), UserBind.class);
            if (userBinds != null && userBinds.size() > 0) {
                return userBinds.get(0);
            }
            return null;
        } else {
            return null;
        }
    }

    public UserBind getUserByUserCode(String userCode) {
        String sql = "select * from f_user_bind t where t.user_code = ? and t.isvalid ='T' ";
        JSONArray jsonArray = DatabaseOptUtils.listObjectsBySqlAsJson(this, sql, new Object[]{userCode});
        if (jsonArray != null && jsonArray.size() > 0) {
            List<UserBind> userBinds = JSONObject.parseArray(jsonArray.toJSONString(), UserBind.class);
            if (userBinds != null && userBinds.size() > 0) {
                return userBinds.get(0);
            }
            return null;
        } else {
            return null;
        }
    }
}
